package service

import (
	"context"
	"fmt"
	"pet-server/basic/config"
	"pet-server/handler/model"
	"time"

	pb "google.golang.org/grpc/examples/helloworld/helloworld"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	pb.UnimplementedGreeterServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) GetUser(_ context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {

	var combo model.Combo

	PetName := ""
	AddressId := 0

	comb := model.Combo{
		Id:        0,
		CreatedAt: time.Time{},
		UpdatedAt: time.Time{},
		DeletedAt: time.Time{},
		Pet:       "",
		Address:   "",
		Store:     "",
		Service:   "",
	}
	fmt.Println(comb)

	//redis
	config.Rdb.Set(config.Ctx, "combo", combo, time.Hour)

	var order model.Order
	order = model.Order{
		Id:        0,
		CreatedAt: time.Time{},
		UpdatedAt: time.Time{},
		DeletedAt: time.Time{},
		PetName:   "",
		AddressId: 0,
		Count:     0,
	}

	if PetName == "" {
		return &pb.HelloReply{Message: "不能为空 " + in.GetName()}, nil
	}

	if AddressId == 0 {
		return &pb.HelloReply{Message: "不能为零 " + in.GetName()}, nil
	}

	err := order.CreateOrder()
	if err != nil {
		return nil, err
	}

	err = order.FirstOrder()
	if err != nil {
		return &pb.HelloReply{Message: "不能重复 " + in.GetName()}, nil

	}

	con := order.FirstCount()
	if con <= 0 {
		return &pb.HelloReply{Message: "库存清空，停止售卖 " + in.GetName()}, nil
	}

	return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}
